﻿#region Using

using Common.Base;
using Common.Enums;

using GameCommon.ComponentInterfaces;
using GameCommon.ManagerInterfaces;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


using GlobalServices = GameCommon.Services.Service;
using GameComponent = GameCommon.Components.GameComponent;

#endregion

namespace GameGraphics.Components
{
    public class Texture : GameComponent, IHitable
    {
        private readonly Texture2D texture2D;

        private HorizontalAlignment horizontalAlignment;

        private VerticalAlignment verticalAlignment;

        private Offset offset;

        public Texture(string id, Texture2D texture)
            : base(id)
        {
            this.Color = Color.White;
            this.texture2D = texture;
            this.Dimension = new Dimension(new Size(texture.Width, texture.Height));
            this.SourceDimension = new Dimension(this.Dimension);
            this.offset = new Offset();
        }

        public Texture(string id, Texture2D texture, GameComponent parent)
            : base(id)
        {
            this.Parent = parent;
            this.Color = Color.White;
            this.texture2D = texture;
            this.Dimension = new Dimension(new Size(texture.Width, texture.Height));
            this.SourceDimension = new Dimension(this.Dimension);
            this.offset = new Offset();
        }
        
        public int Width
        {
            get
            {
                return this.texture2D.Width;
            }
        }

        public int Height
        {
            get
            {
                return this.texture2D.Height;
            }
        }

        public GameComponent Parent { get; private set; }

        public Color Color { get; set; }

        public IHitBox HitBox { get; set; }

        public Dimension SourceDimension { get; set; }

        public VerticalAlignment VerticalAlignment
        {
            get
            {
                return this.verticalAlignment;
            }

            set
            {
                this.verticalAlignment = value;
                this.ApplyAlignment();
            }
        }

        public new Offset Offset
        {
            get
            {
                return this.offset;
            }

            set
            {
                this.offset = value;
                this.ApplyAlignment();
            }
        }

        public HorizontalAlignment HorizontalAlignment
        {
            get
            {
                return this.horizontalAlignment;
            }

            set
            {
                this.horizontalAlignment = value;
                this.ApplyAlignment();
            }
        }

        public override void Draw(IDrawingManager manager, GameTime gameTime)
        {
            this.DrawingService.Draw(manager, this.texture2D, this.Dimension, this.SourceDimension, this.Opacity, this.Color, this.Layer);
        }

        private void ApplyAlignment()
        {
            if (this.Parent != null)
            {
                GlobalServices.ComponentModifier.ApplyAlignments(this.Dimension, this.Parent.Dimension, this.offset, this.horizontalAlignment, this.verticalAlignment);
            }
        }
    }
}